home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / hash.c < prev    next >
C/C++ Source or Header  |  1994-04-16  |  7KB  |  298 lines

  1. /* Hash.c -- Where hashing for bash is done. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. /* There appears to be library functions for this stuff, but it seems like
  22.    a lot of overhead, so I just implemented this hashing stuff on my own. */
  23.  
  24. #if defined (HAVE_STRING_H)
  25. #  include <string.h>
  26. #else /* !HAVE_STRING_H */
  27. #  include <strings.h>
  28. #endif /* !HAVE_STRING_H */
  29.  
  30. #if defined (HAVE_STDLIB_H)
  31. #  include <stdlib.h>
  32. #else
  33. #  include "ansi_stdlib.h"
  34. #endif /* HAVE_STDLIB_H */
  35.  
  36. #include "shell.h"
  37. #include "hash.h"
  38.  
  39. HASH_TABLE *hashed_filenames;
  40.  
  41. #define FILENAME_HASH_BUCKETS 107
  42.  
  43. /* Zero the buckets in TABLE. */
  44. static void
  45. initialize_hash_table (table)
  46.      HASH_TABLE *table;
  47. {
  48.   register int i;
  49.   for (i = 0; i < table->nbuckets; i++)
  50.     table->bucket_array[i] = (BUCKET_CONTENTS *)NULL;
  51. }
  52.  
  53. /* Make a new hash table with BUCKETS number of buckets.  Initialize
  54.    each slot in the table to NULL. */
  55. HASH_TABLE *
  56. make_hash_table (buckets)
  57.      int buckets;
  58. {
  59.   HASH_TABLE *new_table = (HASH_TABLE *)xmalloc (sizeof (HASH_TABLE));
  60.  
  61.   if (buckets == 0)
  62.     buckets = DEFAULT_HASH_BUCKETS;
  63.  
  64.   new_table->bucket_array =
  65.     (BUCKET_CONTENTS **)xmalloc (buckets * sizeof (BUCKET_CONTENTS *));
  66.   new_table->nbuckets = buckets;
  67.   new_table->nentries = 0;
  68.   initialize_hash_table (new_table);
  69.   return (new_table);
  70. }
  71.  
  72. #if 0
  73. /* UNUSED */
  74. /* Create the hash table for filenames that we use in the shell. */
  75. initialize_hashed_filenames ()
  76. {
  77.   hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
  78. }
  79. #endif
  80.  
  81. /* Return the location of the bucket which should contain the data
  82.    for STRING.  TABLE is a pointer to a HASH_TABLE. */
  83.  
  84. #define ALL_ONES (~((unsigned long) 0))
  85. #define BITS(h, n) ((unsigned long)(h) & ~(ALL_ONES << (n)))
  86.  
  87. int
  88. hash_string (string, table)
  89.      char *string;
  90.      HASH_TABLE *table;
  91. {
  92.   register unsigned int i = 0;
  93.  
  94.   while (*string)
  95.     i = (i << 2) + *string++;
  96.  
  97.   return (BITS (i, 31) % table->nbuckets);
  98. }
  99.  
  100. /* Return a pointer to the hashed item, or NULL if the item
  101.    can't be found. */
  102. BUCKET_CONTENTS *
  103. find_hash_item (string, table)
  104.      char *string;
  105.      HASH_TABLE *table;
  106. {
  107.   BUCKET_CONTENTS *list;
  108.   int which_bucket;
  109.  
  110.   if (!table)
  111.     return (BUCKET_CONTENTS *)NULL;
  112.  
  113.   which_bucket = hash_string (string, table);
  114.  
  115.   list = table->bucket_array[which_bucket];
  116.  
  117.   while (list)
  118.     {
  119.       if (STREQ (list->key, string))
  120.     {
  121.       list->times_found++;
  122.       return (list);
  123.     }
  124.       else list = list->next;
  125.     }
  126.   return (BUCKET_CONTENTS *)NULL;
  127. }
  128.  
  129. /* Remove the item specified by STRING from the hash table TABLE.
  130.    The item removed is returned, so you can free its contents.  If
  131.    the item isn't in this table NULL is returned. */
  132. BUCKET_CONTENTS *
  133. remove_hash_item (string, table)
  134.      char *string;
  135.      HASH_TABLE *table;
  136. {
  137.   int the_bucket;
  138.   BUCKET_CONTENTS *prev, *temp;
  139.  
  140.   if (!table)
  141.     return (BUCKET_CONTENTS *)NULL;
  142.  
  143.   the_bucket = hash_string (string, table);
  144.   prev = (BUCKET_CONTENTS *)NULL;
  145.   temp = table->bucket_array[the_bucket];
  146.  
  147.   while (temp)
  148.     {
  149.       if (STREQ (temp->key, string))
  150.     {
  151.       if (prev)
  152.         prev->next = temp->next;
  153.       else
  154.         table->bucket_array[the_bucket] = temp->next;
  155.  
  156.       table->nentries--;
  157.       return (temp);
  158.     }
  159.       prev = temp;
  160.       temp = temp->next;
  161.     }
  162.   return ((BUCKET_CONTENTS *) NULL);
  163. }
  164.  
  165. /* Create an entry for STRING, in TABLE.  If the entry already
  166.    exists, then return it. */
  167. BUCKET_CONTENTS *
  168. add_hash_item (string, table)
  169.      char *string;
  170.      HASH_TABLE *table;
  171. {
  172.   BUCKET_CONTENTS *item;
  173.  
  174.   if (!table)
  175.     table = make_hash_table (0);
  176.  
  177.   if ((item = find_hash_item (string, table)) == 0)
  178.     {
  179.       int bucket = hash_string (string, table);
  180.       item = table->bucket_array[bucket];
  181.  
  182.       while (item && item->next)
  183.     item = item->next;
  184.  
  185.       if (item)
  186.     {
  187.       item->next = (BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
  188.       item = item->next;
  189.     }
  190.       else
  191.     {
  192.       table->bucket_array[bucket] =
  193.         (BUCKET_CONTENTS *)xmalloc (sizeof (BUCKET_CONTENTS));
  194.       item = table->bucket_array[bucket];
  195.     }
  196.  
  197.       item->data = (char *)NULL;
  198.       item->next = (BUCKET_CONTENTS *)NULL;
  199.       item->key = string;
  200.       table->nentries++;
  201.       item->times_found = 0;
  202.     }
  203.  
  204.   return (item);
  205. }
  206.  
  207. /* Return the bucket_contents list of bucket BUCKET in TABLE.  If
  208.    TABLE doesn't have BUCKET buckets, return NULL. */
  209. #undef get_hash_bucket
  210. BUCKET_CONTENTS *
  211. get_hash_bucket (bucket, table)
  212.      int bucket;
  213.      HASH_TABLE *table;
  214. {
  215.   if (table && bucket < table->nbuckets)
  216.     return (table->bucket_array[bucket]);
  217.   else
  218.     return (BUCKET_CONTENTS *)NULL;
  219. }
  220.  
  221. #ifdef TEST_HASHING
  222.  
  223. #undef NULL
  224. #include <stdio.h>
  225.  
  226. HASH_TABLE *table;
  227. #define NBUCKETS 107
  228.  
  229. char *
  230. xmalloc (bytes)
  231.      int bytes;
  232. {
  233.   char *result = (char *)malloc (bytes);
  234.   if (!result)
  235.     {
  236.       fprintf (stderr, "Out of memory!");
  237.       abort ();
  238.     }
  239.   return (result);
  240. }
  241.  
  242. main ()
  243. {
  244.   char string[256];
  245.   int count = 0;
  246.   BUCKET_CONTENTS *tt;
  247.  
  248.   table = make_hash_table (NBUCKETS);
  249.   
  250.   for (;;)
  251.     {
  252.       char *temp_string;
  253.       if (fgets (string, sizeof (string), stdin) == 0)
  254.         break;
  255.       if (!*string)
  256.         break;
  257.       temp_string = savestring (string);
  258.       tt = add_hash_item (temp_string, table);
  259.       if (tt->times_found)
  260.     {
  261.       fprintf (stderr, "You have already added item `%s'\n", string);
  262.       free (temp_string);
  263.     }
  264.       else
  265.     {
  266.       count++;
  267.     }
  268.     }
  269.   
  270.   printf ("You have entered %d (%d) items.  The distribution is:\n",
  271.       table->nentries, count);
  272.  
  273.   /* Print out a count of how many strings hashed to each bucket, so we can
  274.      see how even the distribution is. */
  275.   for (count = 0; count < table->nbuckets; count++)
  276.     {
  277.       int bcount;
  278.       register BUCKET_CONTENTS *list = get_hash_bucket (count, table);
  279.     
  280.       printf ("slot %3d: ", count);
  281.       bcount = 0;
  282.  
  283.       for (bcount = 0; list; list = list->next)
  284.         bcount++;
  285.  
  286.       printf ("%d\n", bcount);
  287.     }
  288.     exit (0);
  289. }
  290.  
  291. #endif /* TEST_HASHING */
  292.  
  293. /*
  294.  * Local variables:
  295.  * compile-command: "gcc -g -DTEST_HASHING -o hash hash.c"
  296.  * end:
  297.  */
  298.